package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.X3DateUtils;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class X3BottomWorkTimeDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private WheelView<String> wv_one;
    private WheelView<String> wv_two;
    private WheelView<String> wv_three;
    private TextView tv_affirm;

    private List<String> oneList;
    private List<String> twoList;
    private List<String> threeList;
    private int hour,minute;
    private List<String> cMinList,cHourList;
    private String selectTime;
    private Calendar calendar;
    private int month,year;

    public X3BottomWorkTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        wv_one = getView(R.id.wv_one);
        wv_two = getView(R.id.wv_two);
        wv_three = getView(R.id.wv_three);
        tv_affirm = getView(R.id.tv_affirm);

        calendar = Calendar.getInstance();
        month = calendar.get(Calendar.MONTH) + 1;
        year = calendar.get(Calendar.YEAR);
        tv_affirm.setOnClickListener(this);

        oneList = new ArrayList<>();
        for (int i = 0;i <= 14; i++) {
            oneList.add(X3DateUtils.getAddDaysMonthDay(i));
        }

        twoList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            twoList.add(i + mContext.getString(R.string.staff_work_time_hour));
        }

        threeList = new ArrayList<>();
        for (int i = 0; i <= 5; i++) {
            threeList.add(i * 10 + mContext.getString(R.string.staff_work_time_minute));
        }

        cHourList = twoList;
        cMinList = threeList;

        wv_one.setOnItemSelectedListener((wheelView, data, position) -> {
            if (position == 0) {
                wv_two.setData(cHourList);
                wv_three.setData(cMinList);
                wv_two.setSelectedItemPosition(0);
                wv_three.setSelectedItemPosition(0);

            }else {
                wv_two.setData(twoList);
                wv_three.setData(threeList);
                wv_two.setSelectedItemPosition(0);
                wv_three.setSelectedItemPosition(0);
            }
        });

    }

    @Override
    public void show() {
        super.show();
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        if (minute >= 50) {

            cHourList = twoList.subList(twoList.indexOf(hour + mContext.getString(R.string.staff_work_time_hour)) + 1,
                    twoList.size());
        }else {
            int i = minute / 10;

            cHourList = twoList.subList(twoList.indexOf(hour + mContext.getString(R.string.staff_work_time_hour)),
                    twoList.size());
            cMinList = threeList.subList(threeList.indexOf(i * 10 + mContext.getString(R.string.staff_work_time_minute)) + 1,
                    threeList.size());

        }
        wv_one.setData(oneList);
        wv_two.setData(cHourList);
        wv_three.setData(cMinList);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_work_time_layout;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_affirm) {
            // 返回选中的时间
            String selectYear = wv_one.getSelectedItemData();
            int selectHour = Integer.parseInt(wv_two.getSelectedItemData().replace(mContext.getString(R.string.staff_work_time_hour), ""));
            int selectMin = Integer.parseInt(wv_three.getSelectedItemData().replace(mContext.getString(R.string.staff_work_time_minute), ""));

            selectTime = selectYear + " " + (selectHour < 10 ? "0" + selectHour : selectHour) + ":" + (selectMin < 10 ? "0" + selectMin : selectMin);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(X3DateUtils.staffStringToDate(selectTime));

            String afterTime = "";
            long afterLongTime = 0;

            int afterMonth = calendar.get(Calendar.MONTH) + 1;
            if (afterMonth < month) {
                calendar.set(Calendar.YEAR,year + 1);
            }else {
                calendar.set(Calendar.YEAR,year);
            }
            afterLongTime = calendar.getTimeInMillis();
            if (iOnSelectTime != null) {
                iOnSelectTime.iOnSelectWorkTime(afterLongTime);
            }
            dismiss();
        }
    }

    IOnSelectTime iOnSelectTime;

    public X3BottomWorkTimeDialog setiOnSelectTime(IOnSelectTime iOnSelectTime) {
        this.iOnSelectTime = iOnSelectTime;
        return this;
    }

    public interface IOnSelectTime {
        void iOnSelectWorkTime(long longTime);
    }

}
